package bit.pja.wolfkill.entity;




import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "room")
public class Room {

    @Id
    @Column(name = "id")
    private Integer roomId;

    @Column(name = "size")
    private int size;

    @Column(name = "created_time")
    private Date createdTime;

    @Column(name = "host_id")
    private Integer hostID;


    @Column(name = "state")
    private Integer state;

    public static final int STATE_READY = 0;
    public static final int STATE_BEGIN = 1;
    public static final int STATE_END = 2;

    private Game game;

    private List<User> users;

    private List<Role> roles;

    public Room() {}

    public Room(Integer roomId, Integer userId, RolesParam param) {
        this.roomId = roomId;
        this.hostID = userId;
        this.users = new ArrayList<>();
        this.roles = new ArrayList<>();

        for(int i=0; i<param.getVillager();i++) {
            roles.add(new Role(RoleId.villager));
        }

        if(param.isHunter()) {
            roles.add(new Role(RoleId.hunter));
        }

        if(param.isProphet()) {
            roles.add(new Role(RoleId.prophet));
        }

        if(param.isWizard()) {
            roles.add(new Role(RoleId.wizard));
        }

        for(int i=0; i<param.getWolf();i++) {
            roles.add(new Role(RoleId.wolf));
        }

        this.state = STATE_READY;

        this.createdTime = new Date();

        this.size = roles.size();

    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public Integer getRoomId() {
        return roomId;
    }

    public void setRoomId(Integer roomId) {
        this.roomId = roomId;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
        if (this.users == null) {
            this.users = new ArrayList<>(size);
        } else {
        }

    }

    public Game getGame() {
        return game;
    }

    public void setGame(Game game) {
        this.game = game;
    }

    public Integer getHostID() {
        return hostID;
    }

    public void setHostID(Integer hostID) {
        this.hostID = hostID;
    }


    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }



    public Room quit(Integer userId) throws Exception{
        for (User user : users) {
            if(user.getId().equals(userId)) {
                users.remove(user);
                if(userId.equals(getHostID())) {
                    if (!users.isEmpty()) {
                        setHostID(users.get(0).getId());
                    }
                }
                return this;
            }
        }
        throw new Exception("用户不在房间");


    }

}
